package com.winderinfo.yikaotianxia.ui.addorder;

import android.view.View;
import android.widget.TextView;

import com.winderinfo.yikaotianxia.R;
import com.winderinfo.yikaotianxia.core.api.Api;
import com.winderinfo.yikaotianxia.core.base.BaseActivity;
import com.winderinfo.yikaotianxia.core.mvp.Contract;
import com.winderinfo.yikaotianxia.core.mvp.Presenter;
import com.winderinfo.yikaotianxia.core.utils.SPUtils;
import com.winderinfo.yikaotianxia.ui.address.AddresListBean;
import com.winderinfo.yikaotianxia.ui.address.AddressActivity;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;


public class AddOrderActivity extends BaseActivity implements Contract.View {

    @BindView(R.id.place_name)
    TextView placeName;
    @BindView(R.id.place_phone)
    TextView placePhone;
    @BindView(R.id.place_address)
    TextView placeAddress;
    private Presenter presenter;
    private Map<String, Object> map = new HashMap<>();
    private String userID;

    @Override
    protected int initLayoutId() {
        return R.layout.activity_placeorder;
    }

    @Override
    protected void initView() {
        presenter = new Presenter(this);
        SPUtils spUtils = new SPUtils(this);
        //获取用户Id
        userID = spUtils.getString("UserID", "");
    }

    @Override
    protected void initData() {
        //我的地址
        /**
         * @userId 用户id
         * */
        map.clear();
        map.put("userId", "1");
        presenter.onPostRequest(Api.ADDRESSLIST, map, AddresListBean.class);
    }


    @Override
    public void onSuccess(Object data) {
        //地址列表
        if (data instanceof AddresListBean) {
            List<AddresListBean.RowsBean> rows = ((AddresListBean) data).getRows();
            for (int i = 0; i < rows.size(); i++) {
                if (rows.get(i).getIsdefault() == "1") {
                    placeName.setText(rows.get(i).getName());
                    placePhone.setText(rows.get(i).getPhone());
                    placeAddress.setText(rows.get(i).getAddr());
                    return;
                }
                if (i == rows.size() - 1) {
                    placeName.setText(rows.get(0).getName());
                    placePhone.setText(rows.get(0).getPhone());
                    placeAddress.setText(rows.get(0).getAddr());
                    return;
                }
            }
        }
    }

    @Override
    public void onFailure(Object error) {

    }


    @OnClick({R.id.place_back, R.id.place_addaddress})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.place_back:
                finish();
                break;
            case R.id.place_addaddress:
                openActivity(AddressActivity.class);
                break;
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        //我的地址
        /**
         * @userId 用户id
         * */
        map.clear();
        map.put("userId", "1");
        presenter.onPostRequest(Api.ADDRESSLIST, map, AddresListBean.class);
    }
}
